package com.jack.leetcode.eachproblem_2020;

public class 反转链表 {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

    public static ListNode reverseList1(ListNode head) {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode next = reverseList(head.next);
        ListNode tmp = next;
        while(tmp.next != null){
            tmp = tmp.next;
        }
        tmp.next = new ListNode(head.val);
        return next;
    }

    public static ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode next = reverseList(head.next);
        ListNode tmp = next;
        while(tmp.next != null){
            tmp = tmp.next;
        }
        tmp.next = new ListNode(head.val);
        return next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        reverseList(head);
    }
}
